package metier;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;
import java.util.ArrayList;

import presentation.ChampionnatInterface;
import presentation.JComboBoxSql;

import com.mysql.jdbc.ResultSet;

import database.Database;

/**
 * Class Championnat
 * Package metier
 * Dfinition d'un Championnat
 * 
 * @author Herv Le Fouler et Lionel Lasry
 */
public class Championnat extends UnicastRemoteObject  implements ChampionnatInterface {
	private int id;
	private String nom;
	private ArrayList<Equipe> listeEquipes;
	
	/**
	 * Constructeur de Championnat
	 * @param String nom du championnat
	 * @param ArrayList<Equipe> listeEquipes une ArrayList d'objets de type Equipe
	 */
	public Championnat(String nom, ArrayList<Equipe> listeEquipes) throws RemoteException{
		this.nom = nom;
		this.listeEquipes = listeEquipes;
	}
	/**
	 * Constructeur de Championnat
	 * @param String nom du championnat
	 */
	public Championnat(String nom) throws RemoteException{
		this.nom = nom;
	}
	/**
	 * Constructeur de Championnat
	 */
	public Championnat() throws RemoteException{
	
	}
	/**
	 * Initialise un objet Championnat en cherchant son id  partir de son nom
	 * @param String nameChampionnat un nom de Championnat
	 * @deprecated Remplac par findChampionnat(String nameChampionnat)
	 */
	public void findChampionnatIdByName(String nameChampionnat) throws RemoteException{
			Database database = new Database();
			database.createConnection();
			ResultSet res = database.returnResultOfQuery("SELECT * from Championnat C where C.nom = '"+nameChampionnat+"'");
			try {
				if ( res != null ){
					res.next();
					this.id = res.getInt("id");
					this.nom = res.getString("nom");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			database.closeConnection();
	}
	
	
	/**
	 * Initialise un objet Championnat en cherchant son id  partir de son nom
	 * Gnre une arrayList d'quipe pour ce championnat
	 * @param String nameChampionnat un nom de Championnat
	 */
	public void findChampionnat(String nameChampionnat) throws RemoteException{
		this.nom = nameChampionnat;
		
		Database database = new Database();
		database.createConnection();
		ResultSet res = database.returnResultOfQuery("SELECT * from Championnat C where C.nom = '"+nameChampionnat+"'");
		try {
			res.next();
			this.id = res.getInt("id");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//ResultSet resEquipe = database.returnResultOfQuery("SELECT E.id FROM equipe E,championnat C where E.id_Championnat = C.id and C.nom = '"+nameChampionnat+"' order by points DESC, goalaverage DESC");
		ResultSet resEquipe = database.returnResultOfQuery("SELECT E.id FROM equipe E,championnat C where E.id_Championnat = C.id and C.nom = '"+nameChampionnat+"'");
		this.listeEquipes = new ArrayList<Equipe>();
		try{
			while (resEquipe.next()){
				Equipe equipe = new Equipe(resEquipe.getInt("id"));
				listeEquipes.add(equipe);
			}
		}
		catch (SQLException ex){ex.printStackTrace ();}
		database.closeConnection();
	}
	
	/**
	 * Initialise un objet Championnat  partir de son id
	 * Gnre une arrayList d'quipe pour ce championnat
	 * @param String nameChampionnat un nom de Championnat
	 */
	public void findChampionnat(int idChampionnat) throws RemoteException{
		Database database = new Database();
		database.createConnection();
		//lancement requete liste equipe d'un championnat par son nom
		ResultSet res = database.returnResultOfQuery("SELECT E.id FROM equipe E,championnat C where E.id_Championnat = C.id and C.id = '"+idChampionnat+"'");
		this.listeEquipes = new ArrayList<Equipe>();
		try{
			while (res.next()){//tant qu'il y'a des lignes
				Equipe equipe = new Equipe(res.getInt("id"));
				listeEquipes.add(equipe);
			}
			ResultSet resChampionnat = database.getAttributesById(idChampionnat,"championnat");
			resChampionnat.next();
			this.nom = resChampionnat.getString("nom");
		}
		
		catch (SQLException ex){ex.printStackTrace ();}
		database.closeConnection();
	}
	
	/**
	 * Cherche l'ensemble des championnats enregistrs
	 * @return ResultSet un resultSet contenant tout les championnats enregistrs dans la base
	 */
	public ResultSet findAllChampionnats() {
		Database database = new Database();
		database.createConnection();
		ResultSet res = database.returnResultOfQuery("SELECT * from Championnat");
		return res;
	}
	/**
	 * Insre un championnat dans la base si le nom donn n'est pas dj enregistr
	 * @param String nom le nom du  championnat que l'on veut crer
	 * @return boolean : True si  l'insertion a t faite, false si le championnat existe dj
	 */
	public boolean createChampionnat(String nom) throws RemoteException{
		Championnat championnat = new Championnat (nom);
		Database database = new Database();
		database.createConnection();
		ResultSet res = this.findAllChampionnats();
		try {
			while (res.next()){//tant qu'il y'a des lignes
				if (nom.compareTo(res.getString("nom")) ==  0){
					database.closeConnection();
					return false;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		database.lancerUpdate("Insert into championnat (id,nom) Values (NULL,'"+nom+"')");
		database.closeConnection();
		return true;
	}
	
	/**
	 * Renvoit le nom d'un championnat
	 * @return String nom
	 */
	public String getNom() throws RemoteException{
		return this.nom;
	}
	/**
	 * initialise le nom d'un championnat
	 * @param String nom
	 */
	public void setNom(String nom) throws RemoteException{
		this.nom = nom;
	}
	/**
	 * Renvoit l'id d'un championnat
	 * @return int id
	 */
	public int getId() throws RemoteException{
		return this.id;
	}
	/**
	 * Renvoit une arrayList d'quipe
	 * @return ArrayList<Equipe> listeEquipes
	 */
	public ArrayList<Equipe> getListeEquipes() throws RemoteException{
		return this.listeEquipes;
	}
}
